#include "Defines.h"
#include "Constants.h"
package 
{
	import flash.events.*;
	import flash.geom.*;
	import flash.utils.*;
	import flash.display.*;
	import flash.system.*;
	import flash.text.*;
	import flash.filters.*;
	import flash.net.*;
	import flash.external.*;
	import flash.ui.*;
	public class CGame 
	{	
		[Embed(mimeType="application/x-font", systemFont="Myriad Pro", fontName="Myriad Pro Condensed")]
		public static const Condensed_My:Class;
		[Embed(mimeType="application/x-font", systemFont="Myriad Pro", fontName="Myriad Pro Condensed",fontWeight="bold")]
		public static const Condensed_My_bold:Class;
		[Embed(mimeType="application/x-font", systemFont="Myriad Pro", fontName="Myriad Pro Bold",fontWeight="bold")]
		public static const BOLD_My:Class;

		public var     		_flag_deg:int;
		public var			_swf_domain:String;
		public var			_game_session_id:String;
		
		public var 			_canvas_rect:Rectangle;
		public var 			_canvas_backbuffer:BitmapData;
		public var			_background_bmp:BitmapData;
		
		public var 			_input_name:TextField = new TextField();
		public var 			_input_pass:TextField = new TextField();
		public var 		    _name_dragon:String ="";
		public static var 	_clip_rect:Rectangle = null;
		
		//private var 			_frame_count:uint = 0;
		public var 				_frame_time:int = 0;
		private var 			_frame_dt:int = 0;
		public var 				_last_frame_time:int = -1;
		
		public var				_is_pause:Boolean = false;
		private var 			_game_state:int;
		private var 			_game_substate:int;
		private var 			_game_state_new:int;
		private var 			_game_state_previous:int;
		
		public var			_key_pressed:Array = new Array(k_NUM_KEYS);
		public var			_key_event_time:Array = new Array(k_NUM_KEYS);
		
		private var			_res_pack:ResourcePack = new ResourcePack("");
		
		private var			_sprites:Array = new Array(NUM_ANIM_FILES);
		private var			_num_loaded_sprite:int = 0;
		
		private var			_texts:Array = new Array(NUM_TEXT_PACKAGES);
		
		public var 			_set_pos_full_screen:Boolean = false;
		
		public  static var  		_w:int = 1024;
		public static var 			_h:int = k_HEIGHT;
		public var _hdc:HDC=null;
		public static var  _app:main = null;
		
		private var sfx:CSound = null;
		private var _sfx:Array = new Array(NUM_SFX);
		private var _music:Array = new Array(NUM_MUSIC);
		public var _id_sound_playing:int = -1;
		
		public static var _mouse_x:int;
		public static var _mouse_y:int;
		public static var _mouse_state:int = k_MOUSE_IDLE;
		public static var _s_delta:int;
		
		public function CGame()
		{
			
		}
		public function Init(app:main):void
		{
			_app = app;
			_hdc = new HDC();
			_canvas_backbuffer = HDC._g;
			//canvas rectangle
			_canvas_rect = new Rectangle(0, 0, k_SCREEN_WIDTH, k_SCREEN_HEIGHT);
			_background_bmp = new BitmapData(k_SCREEN_WIDTH, k_SCREEN_HEIGHT,false,0x006ACB);
			_background_bmp.fillRect(_canvas_rect,  0x006ACB);
			_last_frame_time = -k_FPS_DELAY_TIME;
			
			ClearAllKeyInput();
			InitEx();
		}
		public function BGG():BitmapData
		{
			return _background_bmp;
		}
		public function Init2():void
		{
			_hdc = new HDC();
			_canvas_backbuffer = HDC._g;
			_background_bmp = new BitmapData(k_SCREEN_WIDTH, k_SCREEN_HEIGHT,true,0x006ACB);
			//canvas rectangle
			_canvas_rect = new Rectangle(0, 0, k_SCREEN_WIDTH, k_SCREEN_HEIGHT);
		}
		
		public function Run():void
		{
			_frame_time = getTimer();
			_frame_dt = _frame_time - _last_frame_time;
			
			//var _frame_rate:int = int(1000/_frame_dt);
			//var _str_frame_rate:String = _frame_rate + " fps";
			//var mem:String = Number( System.totalMemory / 1024 / 1024 ).toFixed( 2 ) + "Mb";
			if ((!_is_pause )&& (_frame_dt >= k_FPS_DELAY_TIME))
			{
				//_frame_count++;
				_last_frame_time = _frame_time;
				//lock bitmap data while updating
				_canvas_backbuffer.lock();
				_canvas_rect = new Rectangle(0, 0, k_SCREEN_WIDTH, k_SCREEN_HEIGHT);
				//_canvas_backbuffer.fillRect(_canvas_rect,0x006ACB);
				_canvas_backbuffer.draw(_background_bmp);
				//update and render
				UpdateAndRender();
				//display bitmap data to screen
				_canvas_backbuffer.unlock();
				//if(_frame_rate >= 25)
				//DrawText(TYPE_HP_LOSE,_str_frame_rate,_LEFT,700,50,_BOTTOM,true);
			}
		}
		
		private function IsKeyDown(key_code:int, is_consume:Boolean = false):Boolean
		{
			var ret:Boolean = _key_pressed[key_code];
			
			if (is_consume)
			{
				_key_pressed[key_code] = false;
			}
			return ret;
		}
		
		private function IsAnyKeyDown():Boolean
		{
			var is_pressed:Boolean = false;
			
			for (var i:int = 0; i < k_NUM_KEYS; i++)
			{
				is_pressed = _key_pressed[i];
				
				if (is_pressed)
				{
					return true;
				}
			}
			
			return false;
		}
		private function ClearAllKeyInput():void
		{
			for (var i:int = 0; i < k_NUM_KEYS; i++)
			{
				_key_pressed[i] = false;
				_key_event_time[i] = 0;
			}
		}
		private function GetMouseState(is_consume:Boolean = true):int
		{
			/*var state:int = CLibMouse.s_mouse_state;
			
			if (is_consume)
			{
				CLibMouse.s_mouse_state = CLibMouse.k_MOUSE_IDLE;
			}
			
			return state;*/
			return -1;
		}
		
		private function GetMousePos():Point
		{
			return CLibMouse.s_mouse_pos;
		}

		private function SetGameState(new_state:int, is_destroy:Boolean = true):void
		{
			if (is_destroy)
			{
				_game_state_new = new_state;
				_game_substate = k_DESTROY;
			}
			else
			{
				_game_state_previous = _game_state;
				_game_state = new_state;
				_game_substate = k_INIT;
			}
		}

		private function SetSubState(new_sub_state:int):void
		{
			_game_substate = new_sub_state;
		}
		
		private function LoadSprite(sprite_id:int):void
		{
			var spr:CSprite = _sprites[sprite_id];
			
			if (spr == null)
			{
				spr = new CSprite();
				var bin:ByteArray = _res_pack.GetResource(sprite_id);
				spr.Load(bin);
				_sprites[sprite_id] = spr;
				
				_num_loaded_sprite++;
			}
		}
		
		private function LoadSpritePack(sprite_pack_id:int):void
		{
			var pack_name:String = ResourcePack._ResourceFileName[sprite_pack_id];
			
			var pack:Class;
			pack = _res_pack[pack_name];
			var pack_raw_data:ByteArray = new pack() as ByteArray;
			var pack_data:ByteArray = null;
			
			if (pack_name.indexOf("zip_") == 0)
			{
				pack_data = _res_pack.DecodeLZMA(pack_raw_data);
			}
			else
			{
				pack_data = pack_raw_data;
			}
			pack_data.position = 0;
			
			for (var i:int = 0; i < ResourcePack._IDtoFileName.length; i++)
			{
				if (ResourcePack._IDtoFileName[i] == sprite_pack_id)
				{
					var len:uint = ResourcePack._ResourceInfo[2*i + 1];
					var bin:ByteArray = new ByteArray();
					pack_data.readBytes(bin, 0, len);
					
					var spr:CSprite = _sprites[i];
					if (spr == null)
					{
						spr = new CSprite();
						spr.Load(bin);
						_sprites[i] = spr;
					}
					
				}
			}
		}
		
		private function LoadCurrentDownloadedSprite(sprite_id:int):void
		{
			var spr:CSprite = _sprites[sprite_id];
			
			if (spr == null)
			{
				spr = new CSprite();
				var bin:ByteArray = _res_pack.GetResourceFromDownloadedPack(sprite_id);
				spr.Load(bin);
				_sprites[sprite_id] = spr;
				
				_num_loaded_sprite++;
			}
		}
		
		private function LoadCurrentDownloadedSpritePack():void
		{
			var pack_data:ByteArray = _res_pack._server_res_cache;
			pack_data.position = 0;
			
			for (var i:int = 0; i < ResourcePack._IDtoFileName.length; i++)
			{
				if (ResourcePack._IDtoFileName[i] == _res_pack._downloaded_pack_id)
				{
					var len:uint = ResourcePack._ResourceInfo[2*i + 1];
					var bin:ByteArray = new ByteArray();
					pack_data.readBytes(bin, 0, len);
					
					var spr:CSprite = _sprites[i];
					if (spr == null)
					{
						spr = new CSprite();
						spr.Load(bin);
						_sprites[i] = spr;
					}
					
				}
			}
			
			_res_pack.ClearResourceCache();
		}
		
		private function UnloadSprite(sprite_id:int):void
		{
			var spr:CSprite = _sprites[sprite_id];
			
			if (spr != null)
			{
				spr.Unload();
				_sprites[sprite_id] = null;
				
				_num_loaded_sprite//;
			}
		}
		
		public function GetSprite(sprite_id:int):CSprite
		{
			var spr:CSprite = _sprites[sprite_id];
			
			return spr;
		}
		
		private function LoadTextPack(pack_id:int):void
		{
			var offset:int = 0;
			var text_pack:ByteArray = _res_pack.GetResource(pack_id);
			var encode:int = 0;
			encode = text_pack.readByte()&0xFF;
			//load text pack
			var num_text:int = (text_pack.readByte()&0xFF) | ((text_pack.readByte()&0xFF)<<8);
			_texts[pack_id - FIRST_TEXT_PACKAGE] = new Array(num_text);
			for (var i:int = 0; i < num_text; i++)
			{
				var num_chars:int = (text_pack.readByte()&0xFF) | ((text_pack.readByte()&0xFF)<<8);
				
				var str:String = new String("");
				for (var j:int = 0; j < num_chars; j++)
				{
					if (encode == 1)
					{
						str = str.concat(String.fromCharCode((text_pack.readByte()&0xFF) | ((text_pack.readByte()&0xFF)<<8)));
					}
					else
					{
						str = str.concat(String.fromCharCode(text_pack.readByte()&0xFF));
					}
				}
				_texts[pack_id - FIRST_TEXT_PACKAGE][i] = str;
			}
		}
		
		private function GetText(pack_id:int, text_id:int):String
		{
			var str:String = _texts[pack_id - FIRST_TEXT_PACKAGE][text_id];
			
			return str;
		}
		
		public function SetClip(x:int , y:int, w:int, h:int):void
		{
			HDC.SetClip(x,y,w,h);
		}
		
		public function ClearClip():void
		{
			HDC.ClearClip();
		}
	
		public function fillRect(x:int,y:int,w:int,h:int,c:uint):void
		{
			var doubleBuffer:BitmapData = _canvas_backbuffer.clone();
			doubleBuffer.fillRect(new Rectangle(x, y, w, h),c);
			var transForm:ColorTransform = new ColorTransform(1, 1, 1, 1, 0, 0, 0, 0);
			_canvas_backbuffer.draw(doubleBuffer,null,transForm);
		}
		
		public function drawText(sprite_id:int,str:String,x:int,y:int,anchor:int):void
		{
			GetSprite(sprite_id).DrawText(_canvas_backbuffer,str, x, y, anchor,false);
		}
		
		public function setScreen(w:int,h:int):void
		{
			_w = w;
			_h = h;
		}
		static public function GetScreenWidth():int
		{
			return _w;
		}
		static public function GetScreenHeight():int
		{
			return _h;
		}
		public function GetTextName():String
		{
			return _input_name.text;
		}
		public function GetTextPass():String
		{
			return _input_pass.text;
		}
		public function GetNameDragon():String
		{
			return _name_dragon;
		}
		public 	function AddTextBox(name:String,value:String,type:int,X:int,Y:int,W:int,H:int,anchor:int,center_x:Boolean = false,center_y:Boolean = false):void
		{
			if(anchor == _RIGHT)
			{
				X = DX + X;
			}
			else if(anchor == _BOTTOM)
			{
				Y = DY + Y;
			}
			if(center_x)
			{
				X = (DX>>1) + X;
			}
			if(center_y)
			{
				Y = (DY>>1) + Y;
			}
			if(_app.getChildByName(name) == null)
			{
				var TextBox:TextField = new TextField();
				TextBox.name = name;
				TextBox.wordWrap = true;
				TextBox.type = TextFieldType.INPUT;
				TextBox.x = X;
				TextBox.y = Y;
				
				TextBox.htmlText = value;
				var format:TextFormat;
				switch(type)
				{
					case TYPE_TITLE_DRAGON:
						TextBox.height = 21;
						TextBox.width = W;
						TextBox.backgroundColor = 0xff666666;
						TextBox.border = true;
						format = new TextFormat("Myriad Pro Bold",15,0xFFFFFF);
						format.bold = true;
						TextBox.setTextFormat(format,0,value.length);
					break;
					case TYPE_NAME_ADD_FRIEND:
						TextBox.height = H;
						TextBox.width = W;
						TextBox.backgroundColor = 0xff666666;
						TextBox.border = false;
						format = new TextFormat("Myriad Pro Bold",12,0x000000);
						format.bold = true;
						TextBox.setTextFormat(format,0,value.length);
					break;
				}
				_app.addChild(TextBox);
				TextBox.addEventListener(MouseEvent.MOUSE_DOWN, MouseDown);
			}
			else
			{
				SetText(name,String(TextField(_app.getChildByName(name)).text));
			}
		}
		public function MouseDown(event:MouseEvent):void
		{
			TextField(_app.getChildByName("friend_name")).text = "";
		}
		public function SetText(name:String,value:String):void
		{
			var max:int = 18;
			if(name == "friend_name")
				max = 100;
			if(value.length > max)
			{
				TextField(_app.getChildByName(name)).text = "";
			}
			else
			{
				if(name == "user_name")
				{
					_name_user = value;
				}
				else if(name == "pass")
				{
					_password = value;
				}
				else if(name == "dragon_name")
				{
					_dragon_name = value;
					_name_dragon = value;
				}
				else if(name == "friend_name")
				{
					_name_add_friend = value;
				}
			}
		}
		public function PlaySound(id:int,pos:int=0,loop:Boolean=false):void
		{
			for(var i:int = 0;i<NUM_SFX;i++)
			{
				if(_sfx[i].isPlaying())
					_sfx[i].stop();
			}
			_sfx[id].play(pos,loop);
		}
		public function PlayMusic(id:int,pos:int=0,loop:Boolean=true):void
		{
			_music[id].play(pos,loop);
		}
		public function RemoveTextBox(name:String):void
		{
			_app.removeChild(_app.getChildByName(name));
		}
		public function DrawTextHTML(text:String,align:int,X:int,Y:int,anchor:int,center_x:Boolean = false,center_y:Boolean = false,width_page:int = 0,size:int = 13):void
		{
			if(anchor == _RIGHT)
				X = DX + X;
			else if(anchor == _BOTTOM)
				Y = DY + Y;
			if(center_x)
				X = (DX>>1) + X;
			if(center_y)
				Y = (DY>>1) + Y;
			var textfield:TextField = new TextField();
			textfield.multiline = true;
			textfield.embedFonts = true;
			textfield.wordWrap  = true;
			textfield.htmlText  = text;
			if(width_page!=0){
				textfield.width = width_page;
				textfield.height = (textfield.text.length*size/width_page)*size;
			}
			else{
				textfield.width = textfield.text.length*size;
				textfield.height = size;
			}
			if(align == _HCENTER){
				X = X - (textfield.textWidth>>1);
				textfield.x = X;
				textfield.y = Y;
			}
			else if(align == _RIGHT){
				X = X - textfield.textWidth;
				textfield.x = X;
				textfield.y = Y;
			}
			var color_transform:ColorTransform = null;
			if(HDC.GetOpacity()/100!=1)
			{
				color_transform = new ColorTransform();
				color_transform.alphaMultiplier = HDC.GetOpacity()/100;
				color_transform.alphaOffset = 1;
			}
			var trans_matrix:Matrix = new Matrix(1, 0, 0, 1, X, Y);
			G().draw(textfield,trans_matrix,color_transform, null, CGame._clip_rect,true);
		}
		public  function DrawText(type:int,text:String,align:int,X:int,Y:int,anchor:int,center_x:Boolean = false,center_y:Boolean = false,width_page:int = 0):void
		{
			if(anchor == _RIGHT){
				X = DX + X;
			}
			else if(anchor == _BOTTOM){
				Y = DY + Y;
			}
			if(center_x){
				X = (DX>>1) + X;
			}
			if(center_y){
				Y = (DY>>1) + Y;
			}
			var textfield:TextField = new TextField();
			textfield.htmlText  = text;
            //textfield.height = 200;
            textfield.multiline = true;
			textfield.embedFonts = true;
			textfield.wordWrap  = true;
			//textfield.border = true;
			var alpha:Boolean = false;
			var format:TextFormat;
			var text_format:String = "Myriad Pro Condensed";
			var size:Number = 15;
			var Color:Number = 0x000000;
			var bold:Boolean = false;
			var italic:Boolean = false;
			var leading:int = 0;
			switch (type)
			{
				case TYPE_SHOP_TITLE:
					text_format = "Myriad Pro Condensed";
					size = 16;
					Color = 0x000000;
				break;
				case TYPE_WARNING_NEW:
					text_format = "Myriad Pro Bold";
					size = 17;
					Color = 0xFFFFFF00;
					bold = true;
				break;
				case TYPE_QUEST_REWARD:
					text_format = "Myriad Pro Condensed";
					size = 19;
					Color = 0xFFFFFFFF;
					textfield.antiAliasType = AntiAliasType.ADVANCED;
					textfield.autoSize = TextFieldAutoSize.CENTER;
					textfield.filters = [new GlowFilter(0x0F687B, 1.0, 6, 6, 5)];
				break;
				case TYPE_SHOP_TIME:
					text_format = "Myriad Pro Condensed";
					size = 17;
					Color = 0xFFFF6600;
					bold = true;
				break;
				case TYPE_ITEM_QUEST:
					text_format = "Myriad Pro Condensed";
					size = 18;
					Color = 0x000000;
				break;
				case TYPE_NORMAL_YELLOW:
					text_format = "Myriad Pro Condensed";
					size = 17;
					Color = 0xFFEACA15;
					bold = true;
				break;				
				case TYPE_SHOP_EXP:
					text_format = "Myriad Pro Condensed";
					size = 15;
					Color = 0xFFFFC000;
					bold = true;
				break;
				case TYPE_SHOP_FOOD:
					text_format = "Myriad Pro Condensed";
					size = 15;
					Color = 0xFFFFFFFF;
					bold = true;
				break;
				case TYPE_INTERFACE_HAB:
					text_format = "Myriad Pro Condensed";
					size = 21;
					Color = 0xFF845E3C;
				break;
				case TYPE_PROCESS_FEED:
					text_format = "Myriad Pro Condensed";
					size = 13;
					Color = 0x000000;
					bold = true;
					italic = true;
				break;
				case TYPE_HP_TIMEBAR:
					text_format = "Myriad Pro Condensed";
					size = 11;
					Color = 0x000000;
					bold = true;
				break;
				case TYPE_LOGIN:
					text_format = "Myriad Pro Condensed";
					size = 13;
					Color = 0x000000;
					bold = true;
					italic = true;
				break;
				case TYPE_LOGIN_SELECT:
					text_format = "Myriad Pro Condensed";
					size = 13;
					Color = 0x7FFF00;
					bold = true;
					italic = true;
				break;
				
				case TYPE_PROCESS_FEED_2:
					text_format = "Myriad Pro Condensed";
					size = 15;
					Color = 0xFFFFFFFF;
					bold = true;
				break;
				case TYPE_SHOP_GEM:
					text_format = "Myriad Pro Condensed";
					size = 22;
					Color = 0xFFFFFF00;
					bold = true;
				break;	
				case TYPE_SHOP_INFO:
					text_format = "Myriad Pro Condensed";
					size = 15;
					Color =0x336600;
				break;
				case TYPE_SHOP_LIMIT:
					text_format = "Myriad Pro Bold";
					size = 15;
					Color = 0xF0F8FF;
					bold = true;
					textfield.autoSize = TextFieldAutoSize.CENTER;
					textfield.filters = [new GlowFilter(0x000000, 1.0,3,3,4)];
				break;
				case TYPE_FARM_INFO:
					text_format = "Myriad Pro Condensed";
					size = 16;
					Color =0x336600;
				break;
				case TYPE_REWARD:
					text_format = "Myriad Pro Condensed";
					size = 20;
					Color = 0xffffff;
					textfield.autoSize = TextFieldAutoSize.CENTER;
					textfield.filters = [new GlowFilter(0x002b5f, 1.0,4,4,10)];
				break;	
				case TYPE_LEVEL_UP:
					text_format = "Myriad Pro Condensed";
					size = 22;
					Color = 0x783e00;
				break;
				case TYPE_ID_MAIN_QUEST:
					text_format = "Myriad Pro Condensed";
					size = 16;
					bold = true;
					Color = 0xffffff;
					textfield.autoSize = TextFieldAutoSize.CENTER;
					textfield.filters = [new GlowFilter(0x002b5f, 1.0,4,4,10)];
				break;	
				case TYPE_BREED_NAME:
					text_format = "Myriad Pro Bold";
					size = 15;
					bold = true;
					Color = 0x6a4100;
				break;
				case TYPE_BREED_NAME_EV:
					text_format = "Myriad Pro Condensed";
					size = 17;
					Color = 0x336600;
				break;
				case TYPE_BREED_LEVEL:
					text_format = "Myriad Pro Condensed";
					size = 18;
					Color = 0x336600;
				break;
				case TYPE_CONFIRM_TIME:
					text_format = "Myriad Pro Condensed";
					size = 17;
					Color = 0x000000;
				break;
				case TYPE_CONFIRM_TEXT:
					text_format = "Myriad Pro Condensed";
					size = 18;
					Color = 0x783e00;
				break;
				case TYPE_SHOP_PRICE:
					text_format = "Myriad Pro Condensed";
					size = 14;
					Color = 0xFFFFFFFF;
					textfield.antiAliasType = AntiAliasType.ADVANCED;
					textfield.autoSize = TextFieldAutoSize.CENTER;
					textfield.filters = [new GlowFilter(0xFF000000, 1.0, 3, 3, 4)];
				break;
				case TYPE_SHOP_NAME:
					text_format = "Myriad Pro Bold";
					size = 16;
					Color = 0xFFFFFF;
					textfield.antiAliasType = AntiAliasType.ADVANCED;
					textfield.autoSize = TextFieldAutoSize.CENTER;
					textfield.filters = [new GlowFilter(0x90580C, 1.0, 4, 4, 5)];
				break;
				case TYPE_COLLECT_COIN:
					text_format = "Myriad Pro Bold";
					size = 14;
					Color = 0xFFFFFF;
					textfield.antiAliasType = AntiAliasType.ADVANCED;
					textfield.autoSize = TextFieldAutoSize.CENTER;
					textfield.filters = [new GlowFilter(0x90580C, 1.0, 4, 4, 5)];
				break;
				case TYPE_NOTE_IN_MENU:
					text_format = "Myriad Pro Condensed";
					size = 16;
					Color = 0xFFFFFF;
					textfield.antiAliasType = AntiAliasType.ADVANCED;
					textfield.autoSize = TextFieldAutoSize.CENTER;
					textfield.filters = [new GlowFilter(0x90580C, 1.0, 4, 4, 5)];
				break;
				case TYPE_WARNING:
					text_format = "Myriad Pro Condensed";
					size = 20;
					Color = 0xFFFFFF;
					textfield.antiAliasType = AntiAliasType.ADVANCED;
					textfield.autoSize = TextFieldAutoSize.CENTER;
					textfield.filters = [new GlowFilter(0xDD0000, 1.0, 4, 4, 5)];
				break;
				case TYPE_MENU_NAME:
					text_format = "Myriad Pro Condensed";
					size = 13;
					Color = 0xFFFFFF;
					textfield.antiAliasType = AntiAliasType.ADVANCED;
					textfield.autoSize = TextFieldAutoSize.CENTER;
					textfield.filters = [new GlowFilter(0x000000, 1.0, 4, 4, 4)];
				break;
				case TYPE_INFO_USER:
					text_format = "Myriad Pro Bold";
					size = 16;
					Color = 0x000000;
					bold = true;
					textfield.antiAliasType = AntiAliasType.ADVANCED;
					textfield.autoSize = TextFieldAutoSize.CENTER;
					textfield.filters = [new GlowFilter(0xFFFFFF, 1.0, 3, 3, 3)];
				break;
				case TYPE_USER_HUD_NAME:
					text_format = "Myriad Pro Bold";
					size = 18;
					Color = 0xFFFFFF;
					bold = true;
					textfield.antiAliasType = AntiAliasType.ADVANCED;
					textfield.autoSize = TextFieldAutoSize.CENTER;
					textfield.filters = [new GlowFilter(0x000000, 1.0, 3, 3, 3)];
				break;
				case TYPE_USER_HUD_LEVEL:
					text_format = "Myriad Pro Bold";
					size = 16;
					Color = 0xFFFFFF;
					bold = true;
					italic = true;
					textfield.antiAliasType = AntiAliasType.ADVANCED;
					textfield.autoSize = TextFieldAutoSize.CENTER;
					textfield.filters = [new GlowFilter(0x000000, 1.0, 4, 4, 4)];
				break;
				case TYPE_FRIEND_NAME:
					text_format = "Myriad Pro Bold";
					size = 14;
					bold = true;
					Color = 0xFFFFFF;
					textfield.antiAliasType = AntiAliasType.ADVANCED;
					textfield.autoSize = TextFieldAutoSize.CENTER;
					textfield.filters = [new GlowFilter(0x000000,1.0,2,2,4)];
				break;
				case TYPE_FRIEND_LEVEL:
					text_format = "Myriad Pro Condensed";
					size = 11;
					Color = 0x000000;
				break;
				case TYPE_FRIEND_EXP:
					text_format = "Myriad Pro Condensed";
					size = 11.5;
					Color = 0xFF6495ED;
				break;
				case TYPE_TITLE_DRAGON:
					text_format = "Myriad Pro Bold";
					size = 19;
					Color = 0xFFFFFF;
					bold = true;
				break;
				case TYPE_INFO_DRAGON:
					text_format = "Myriad Pro Condensed";
					size = 17;
					Color = 0x783e00;
				break;
				case TYPE_COMBAT_MONSTERNAME:
					text_format = "Myriad Pro Condensed";
					size = 16;
					Color = 0xFF7C3104;
				break;
				case TYPE_COMBAT_USERNAME:
					text_format = "Myriad Pro Bold";
					size = 23;
					Color = 0xFF7C3104;
					bold = true;
				break;
				case TYPE_COMBAT_TIMER:
					text_format = "Myriad Pro Bold";
					size = 16;
					Color = 0xFF7C3104;
					bold = true;
				break;
				case TYPE_BREED_RATIO:
					text_format = "Myriad Pro Bold";
					size = 16;
					Color = 0xFFFFFF;
					bold = true;
				break;
				case TYPE_COMBAT_HP_BAR:
					text_format = "Myriad Pro Bold";
					size = 19;
					Color = 0xFFFFFFFF;
					bold = true;
					textfield.antiAliasType = AntiAliasType.ADVANCED;
					textfield.autoSize = TextFieldAutoSize.CENTER;
					textfield.filters = [new GlowFilter(0x530500, 1.0, 3, 3, 10)];
				break;
				case TYPE_COLLECT_DRAGON:
					text_format = "Myriad Pro Bold";
					size = 16;
					Color = 0x000000;
				break;
				case TYPE_MONSTER_TIME_WAIT:
					text_format = "Myriad Pro Bold";
					size = 16;
					Color = 0x000000;
					bold = true;
				break;
				case TYPE_MONSTER_TIME_WARNING:
					text_format = "Myriad Pro Bold";
					size = 16;
					Color = 0xFF0000;
					bold = true;
				break;
				
				case TYPE_SHOP_LOCK:
					text_format = "Myriad Pro Bold";
					size = 21;
					Color = 0xFFFFFFFF;
					bold = true;
					textfield.antiAliasType = AntiAliasType.ADVANCED;
					textfield.autoSize = TextFieldAutoSize.CENTER;
					textfield.filters = [new GlowFilter(0x000000, 1.0, 3, 3, 4)];
					alpha = true;
				break;
				case TYPE_HP_LOSE:
					text_format = "Myriad Pro Bold";
					size = 42;
					Color = 0xFFCC0000;
					textfield.antiAliasType = AntiAliasType.ADVANCED;
					textfield.autoSize = TextFieldAutoSize.CENTER;
					textfield.filters = [new GlowFilter(0x000000, 1.0, 3, 3, 10)];
					alpha = true;
				break;
				case TYPE_HP_ADD:
					text_format = "Myriad Pro Bold";
					size = 42;
					Color = 0xFF33FF33;
					textfield.antiAliasType = AntiAliasType.ADVANCED;
					textfield.autoSize = TextFieldAutoSize.CENTER;
					textfield.filters = [new GlowFilter(0x000000, 1.0, 3, 3, 10)];
					alpha = true;
				break;
				case TYPE_HINT_COMBAT:
					text_format = "Myriad Pro Bold";
					size = 20;
					Color = 0xFF33FF33;
					textfield.antiAliasType = AntiAliasType.ADVANCED;
					textfield.autoSize = TextFieldAutoSize.CENTER;
					textfield.filters = [new GlowFilter(0x000000, 1.0, 3, 3, 10)];
					alpha = true;
				break;
				case TYPE_COIN_COLLECT:
					text_format = "Myriad Pro Bold";
					size = 22;
					Color = 0xFFFF9500;
					textfield.antiAliasType = AntiAliasType.ADVANCED;
					textfield.autoSize = TextFieldAutoSize.CENTER;
					textfield.filters = [new GlowFilter(0x000000, 1.0, 3, 3, 10)];
					alpha = true;
				break;
				case TYPE_BUTTON_RESEARCH:
					text_format = "Myriad Pro Condensed";
					size = 13;
					Color = 0xFFFFFF;
					textfield.antiAliasType = AntiAliasType.ADVANCED;
					textfield.autoSize = TextFieldAutoSize.CENTER;
					textfield.filters = [new GlowFilter(0x000000, 1.0, 3, 3, 4)];
				break;
				case TYPE_NUM_RANK:
					text_format = "Myriad Pro Bold";
					size = 20;
					Color = 0xFFFFFF;
					textfield.antiAliasType = AntiAliasType.ADVANCED;
					textfield.autoSize = TextFieldAutoSize.CENTER;
					textfield.filters = [new GlowFilter(0x8e5027, 1.0, 4, 4, 4)];
				break;
				case TYPE_USERNAME_RANK:
					text_format = "Myriad Pro Condensed";
					size = 16;
					Color = 0xfff600;
					textfield.antiAliasType = AntiAliasType.ADVANCED;
					textfield.autoSize = TextFieldAutoSize.CENTER;
					textfield.filters = [new GlowFilter(0x674100, 1.0, 4, 4, 4)];
				break;
				case TYPE_USERNAME_RANK_M:
					text_format = "Myriad Pro Bold";
					size = 17;
					Color = 0xfff600;
					textfield.antiAliasType = AntiAliasType.ADVANCED;
					textfield.autoSize = TextFieldAutoSize.CENTER;
					textfield.filters = [new GlowFilter(0x674100, 1.0, 4, 4, 4)];
				break;
				case TYPE_EXP_RANK:
					text_format = "Myriad Pro Bold";
					size = 18;
					Color = 0xFFFFFF;
					textfield.antiAliasType = AntiAliasType.ADVANCED;
					textfield.autoSize = TextFieldAutoSize.CENTER;
					textfield.filters = [new GlowFilter(0x8e5027, 1.0, 4, 4, 4)];
				break;
				case TYPE_POPUP:
					text_format = "Myriad Pro Condensed";
					size = 18;
					Color = 0x703200;
					leading = 5;
				break;
				case TYPE_NAME_MONSTER_YEUCAU:
					text_format = "Myriad Pro Condensed";
					size = 20;
					Color = 0x783e00;
					bold = true;
				break;
				case TYPE_NAME_MONSTER_TRANSFORM_YEUCAU:
					text_format = "Myriad Pro Condensed";
					size = 16;
					Color = 0x000000;
				break;
				case TYPE_NAME_IN_BREED_HIS:
					text_format = "Myriad Pro Condensed";
					size = 15;
					Color = 0x8E4700;
				break;
				case TYPE_TIME_BUY_MAP:
					text_format = "Myriad Pro Condensed";
					size = 13;
					bold = true;
					Color = 0xffffff;
				break;
				case TYPE_NAME_DAD_IN_BREED_HIS:
					text_format = "Myriad Pro Condensed";
					size = 15;
					Color = 0x8E4700;
				break;
				case TYPE_INFO_IN_BREED_HIS:
					text_format = "Myriad Pro Condensed";
					size = 17;
					Color = 0xE00600;
				break;
				case TYPE_INFO_MONSTER_COLLECT:
					text_format = "Myriad Pro Condensed";
					size = 15;
					Color = 0xE00600;
				break;
				case TYPE_NOTE_IN_BREED_HIS:
					text_format = "Myriad Pro Condensed";
					size = 17;
					Color = 0x8E4700;
				break;
				case TYPE_PAGE_IN_BREED_HIS:
					text_format = "Myriad Pro Condensed";
					size = 17;
					Color = 0x8E4700;
				break;
				case TYPE_NAME_MONSTER_TRANSFORM:
					text_format = "Myriad Pro Condensed";
					size = 17;
					Color = 0xFFFFFF;
					bold = true;
				break;
				case TYPE_NAME_MONSTER_RANK:
					text_format = "Myriad Pro Bold";
					size = 13;
					Color = 0x00000000;
				break;
				case TYPE_NAME_MONSTER_RANK_NO:
					text_format = "Myriad Pro Condensed";
					size = 13;
					bold = false;
					Color = 0x00000000;
				break;
				case TYPE_FOUND_FRIEND:
					text_format = "Myriad Pro Bold";
					size = 42;
					Color = 0xFFFF00;
				break;
				case TYPE_RS_FINISHED:
					text_format = "Myriad Pro Bold";
					size = 17;
					Color = 0x86591d;
				break;
				case TYPE_RS_FINISHED_NOTE:
					text_format = "Myriad Pro Condensed";
					size = 16;
					Color = 0x86591d;
				break;
				case TYPE_INFO_TITLE_DRAGON:
					text_format = "Myriad Pro Condensed";
					size = 14;
					Color = 0x004c78;
					bold = true;
					textfield.autoSize = TextFieldAutoSize.CENTER;
					textfield.filters = [new GlowFilter(0xffffff, 1.0, 3, 3, 4)];
				break;
				case TYPE_COLLECT_TIME_DRAGON:
					text_format = "DejaVu Sans";
					size = 11;
					Color = 0x783e00;
					bold = true;	
				break;
				case TYPE_EXP_RANK:
					text_format = "Myriad Pro Condensed";
					size = 18;
					Color = 0xffffff;
					textfield.autoSize = TextFieldAutoSize.CENTER;
					textfield.filters = [new GlowFilter(0x884d00, 1.0, 4, 4, 4)];
				break;
				case TYPE_USERNAME_RANK:
					text_format = "Myriad Pro Condensed";
					size = 16;
					Color = 0xfef500;
					textfield.autoSize = TextFieldAutoSize.CENTER;
					textfield.filters = [new GlowFilter(0x884d00, 1.0, 4, 4, 4)];
				break;
				case TYPE_USERNAME_RANK:
					text_format = "Myriad Pro Condensed";
					size = 12;
					Color = 0x6a4100;
					bold = true;	
				break;
				case TYPE_INFO_MONSTER_FOOD:
					text_format = "Myriad Pro Condensed";
					size = 12;
					Color = 0x000000;	
				break;
				case TYPE_TIME_DRAGON:
					text_format = "Myriad Pro Condensed";
					size = 13;
					Color = 0x783e00;
				break;
				case TYPE_CO_THE_NGHIEN_CUU:
					text_format = "Myriad Pro Condensed";
					size = 17;
					Color = 0xaa00b2;
				break;
				default:
					text_format = "Myriad Pro Condensed";
					size = 16;
					Color = 0xFF097ab8;
				break;
			}
			format = new TextFormat(text_format,size,Color,bold,italic);
			format.leading = leading;
			if(width_page!=0)
			{
				textfield.width = width_page;
				if(text.length*size/width_page >= 1)
					textfield.height = (text.length*size/width_page)*size;
				else
					textfield.height = size;
			}
			else
			{
				textfield.width = text.length * size;
				textfield.height = size;
			}
			if(align == _HCENTER)
				format.align = TextFormatAlign.CENTER;
			else if(align == _RIGHT)
				format.align = TextFormatAlign.RIGHT;
			textfield.setTextFormat(format,0,text.length);
			if(align == _HCENTER)
			{
				X = X - textfield.width/2;
				Y = Y - textfield.textHeight*0.7;
			}
			else if(align == _RIGHT)
			{
				X = X - textfield.width;
			}
			if(alpha)
			{
				var color_transform:ColorTransform = new ColorTransform();
				var opacity:Number = HDC.GetOpacity()/100;
				color_transform.alphaMultiplier = opacity;
				color_transform.alphaOffset = 1;
				var trans_matrix:Matrix = new Matrix(1, 0, 0, 1, X, Y);
				HDC.FlushMatrix(trans_matrix);
				G().draw(textfield,trans_matrix,color_transform, null, CGame._clip_rect, true);
			}
			else
			{
				trans_matrix = new Matrix(1, 0, 0, 1, X, Y);
				HDC.FlushMatrix(trans_matrix);
				G().draw(textfield,trans_matrix,null, null, CGame._clip_rect, true);
			}
		}
		#include "Game_Include.h"
		public function renderBG():void{
			if(_map != null){
				_map.Render(0,0);
			}
		}
	}
}